home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / cool.lha / ice / pisces / mkdepend / eval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  27.7 KB  |  1,176 lines

  1. /*
  2.  *  Expression Evaluation
  3.  *
  4.  * Edit history
  5.  * Created: MJF  07-Dec-90 -- originally from cpp5.c
  6.  *
  7.  */
  8.  
  9.  
  10. #include    <stdio.h>
  11. #include    <ctype.h>
  12. #include    "def.h"
  13. /*
  14.  * from cppdef.h
  15.  */
  16. #define    FILE_LOCAL        /* static or global */
  17. #define    NEXP        128
  18. #define ALERT        '\007'    /* ASCII  '\a' Bell */
  19. #define    VT        '\013'    /* Vertical Tab CTRL/K    */
  20. #define    BITS_CHAR    8
  21.  
  22. #ifndef    TRUE
  23. #define    TRUE        1
  24. #define    FALSE        0
  25. #endif
  26.  
  27. /*
  28.  * from cpp.h
  29.  */
  30. int instring = FALSE;
  31. #define    streq(s1, s2)    (strcmp(s1, s2) == 0)
  32.  
  33. #define    TOK_SEP        0x1E        /* Token concatenation delim.    */
  34. #define    EOF_CHAR    0        /* Returned by get() on eof    */
  35. #define NULLST        ((char *) NULL)    /* Pointer to nowhere (linted)    */
  36. #define    EOS        '\0'        /* End of string        */
  37.  
  38. /*
  39.  * Character type codes.
  40.  */
  41.  
  42. #define    INV        0        /* Invalid, must be zero    */
  43. #define    OP_EOE        INV        /* End of expression        */
  44. #define    DIG        1        /* Digit            */
  45. #define    LET        2        /* Identifier start        */
  46. #define    FIRST_BINOP    OP_ADD
  47. #define    OP_ADD        3
  48. #define    OP_SUB        4
  49. #define    OP_MUL        5
  50. #define    OP_DIV        6
  51. #define    OP_MOD        7
  52. #define    OP_ASL        8
  53. #define    OP_ASR        9
  54. #define    OP_AND        10        /* &, not &&            */
  55. #define    OP_OR        11        /* |, not ||            */
  56. #define    OP_XOR        12
  57. #define    OP_EQ        13
  58. #define    OP_NE        14
  59. #define    OP_LT        15
  60. #define    OP_LE        16
  61. #define    OP_GE        17
  62. #define    OP_GT        18
  63. #define    OP_ANA        19        /* &&                */
  64. #define    OP_ORO        20        /* ||                */
  65. #define    OP_QUE        21        /* ?                */
  66. #define    OP_COL        22        /* :                */
  67. #define    OP_CMA        23        /* , (relevant?)        */
  68. #define    LAST_BINOP    OP_CMA        /* Last binary operand        */
  69. /*
  70.  * The following are unary.
  71.  */
  72. #define    FIRST_UNOP    OP_PLU        /* First Unary operand        */
  73. #define    OP_PLU        24        /* + (draft ANSI standard)    */
  74. #define    OP_NEG        25        /* -                */
  75. #define    OP_COM        26        /* ~                */
  76. #define    OP_NOT        27        /* !                */
  77. #define    LAST_UNOP    OP_NOT
  78. #define    OP_LPA        28        /* (                */
  79. #define    OP_RPA        29        /* )                */
  80. #define    OP_END        30        /* End of expression marker    */
  81. #define    OP_MAX        (OP_END + 1)    /* Number of operators        */
  82. #define    OP_FAIL        (OP_END + 1)    /* For error returns        */
  83.  
  84. /*
  85.  * The following are for lexical scanning only.
  86.  */
  87.  
  88. #define    QUO        65        /* Both flavors of quotation    */
  89. #define    DOT        66        /* . might start a number    */
  90. #define    SPA        67        /* Space and tab        */
  91. #define    BSH        68        /* Just a backslash        */
  92. #define    END        69        /* EOF                */
  93.  
  94. /*
  95.  * These bits are set in ifstack[]
  96.  */
  97. #define    WAS_COMPILING    1        /* TRUE if compile set at entry    */
  98. #define    ELSE_SEEN    2        /* TRUE when #else processed    */
  99. #define    TRUE_SEEN    4        /* TRUE when #if TRUE processed    */
  100.  
  101. /*
  102.  * Define bits for the basic types and their adjectives
  103.  */
  104.  
  105. #define    T_CHAR          1
  106. #define    T_INT          2
  107. #define    T_FLOAT          4
  108. #define    T_DOUBLE      8
  109. #define    T_SHORT         16
  110. #define    T_LONG         32
  111. #define    T_SIGNED     64
  112. #define    T_UNSIGNED    128
  113. #define    T_PTR        256        /* Pointer            */
  114. #define    T_FPTR        512        /* Pointer to functions        */
  115.  
  116. /*
  117.  * The SIZES structure is used to store the values for #if sizeof
  118.  */
  119.  
  120. typedef struct sizes {
  121.     short    bits;            /* If this bit is set,        */
  122.     short    size;            /* this is the datum size value    */
  123.     short    psize;            /* this is the pointer size    */
  124. } SIZES;
  125.  
  126. /* end of cpp.h */
  127.  
  128. /* from cpp6.c */
  129. #define    DOL    LET
  130.  
  131. char type[256] = {    /* Character type codes ASCII      Hex          */ 
  132.    END,   000,   000,   000,   000,   000,   000,   000, /* 00        */
  133.    000,   SPA,   000,   000,   000,   000,   000,   000, /* 08        */
  134.    000,   000,   000,   000,   000,   000,   000,   000, /* 10        */
  135.    000,   000,   000,   000,   000,   LET,   000,   SPA, /* 18        */
  136.    SPA,OP_NOT,   QUO,   000,   DOL,OP_MOD,OP_AND,   QUO, /* 20  !"#$%&'    */
  137. OP_LPA,OP_RPA,OP_MUL,OP_ADD,   000,OP_SUB,   DOT,OP_DIV, /* 28 ()*+,-./    */
  138.    DIG,   DIG,   DIG,   DIG,   DIG,   DIG,   DIG,   DIG, /* 30 01234567    */
  139.    DIG,   DIG,OP_COL,   000, OP_LT, OP_EQ, OP_GT,OP_QUE, /* 38 89:;<=>?    */
  140.    000,   LET,   LET,   LET,   LET,   LET,   LET,   LET, /* 40 @ABCDEFG    */
  141.    LET,   LET,   LET,   LET,   LET,   LET,   LET,   LET, /* 48 HIJKLMNO    */
  142.    LET,   LET,   LET,   LET,   LET,   LET,   LET,   LET, /* 50 PQRSTUVW    */
  143.    LET,   LET,   LET,   000,   BSH,   000,OP_XOR,   LET, /* 58 XYZ[\]^_    */
  144.    000,   LET,   LET,   LET,   LET,   LET,   LET,   LET, /* 60 `abcdefg    */
  145.    LET,   LET,   LET,   LET,   LET,   LET,   LET,   LET, /* 68 hijklmno    */
  146.    LET,   LET,   LET,   LET,   LET,   LET,   LET,   LET, /* 70 pqrstuvw    */
  147.    LET,   LET,   LET,   000, OP_OR,   000,OP_NOT,   000, /* 78 xyz{|}~    */
  148.    000,   000,   000,   000,   000,   000,   000,   000, /* 80(reserved)*/
  149.    000,   000,   000,   000,   000,   000,   000,   000, /* 88(   for  )*/
  150.    000,   000,   000,   000,   000,   000,   000,   000, /* 90(MAC_PARM)*/
  151.    000,   000,   000,   000,   000,   000,   000,   000, /* 98(  table )*/
  152.    000,   000,   000,   000,   000,   000,   000,   000, /* A0            */
  153.    000,   000,   000,   000,   000,   000,   000,   000, /* A8            */
  154.    000,   000,   000,   000,   000,   000,   000,   000, /* B0            */
  155.    000,   000,   000,   000,   000,   000,   000,   000, /* B8            */
  156.    000,   000,   000,   000,   000,   000,   000,   000, /* C0   not      */
  157.    000,   000,   000,   000,   000,   000,   000,   000, /* C8   used     */
  158.    000,   000,   000,   000,   000,   000,   000,   000, /* D0            */
  159.    000,   000,   000,   000,   000,   000,   000,   000, /* D8            */
  160.    000,   000,   000,   000,   000,   000,   000,   000, /* E0            */
  161.    000,   000,   000,   000,   000,   000,   000,   000, /* E8            */
  162.    000,   000,   000,   000,   000,   000,   000,   000, /* F0            */
  163.    000,   000,   000,   000,   000,   000,   000,   000, /* F8            */
  164. };
  165.  
  166. /* end of cpp6.c */
  167.  
  168. char tokenbuf[256];
  169.  
  170. /*
  171.  * Evaluate a #if expression.
  172.  */
  173.  
  174. static char    *opname[] = {        /* For debug and error messages    */
  175. "end of expression", "val", "id",
  176.   "+",   "-",  "*",  "/",  "%",
  177.   "<<", ">>",  "&",  "|",  "^",
  178.   "==", "!=",  "<", "<=", ">=",  ">",
  179.   "&&", "||",  "?",  ":",  ",",
  180.   "unary +", "unary -", "~", "!",  "(",  ")", "(none)",
  181. };
  182.  
  183. /*
  184.  * opdope[] has the operator precedence:
  185.  *     Bits
  186.  *      7    Unused (so the value is always positive)
  187.  *    6-2    Precedence (000x .. 017x)
  188.  *    1-0    Binary op. flags:
  189.  *     01    The binop flag should be set/cleared when this op is seen.
  190.  *     10    The new value of the binop flag.
  191.  * Note:  Expected, New binop
  192.  * constant    0    1    Binop, end, or ) should follow constants
  193.  * End of line    1    0    End may not be preceeded by an operator
  194.  * binary    1    0    Binary op follows a value, value follows.
  195.  * unary    0    0    Unary op doesn't follow a value, value follows
  196.  *   (        0    0    Doesn't follow value, value or unop follows
  197.  *   )        1    1    Follows value.  Op follows.
  198.  */
  199.  
  200. static char    opdope[OP_MAX] = {
  201.   0001,                    /* End of expression        */
  202.   0002,                    /* Digit            */
  203.   0000,                    /* Letter (identifier)        */
  204.   0141, 0141, 0151, 0151, 0151,        /* ADD, SUB, MUL, DIV, MOD    */
  205.   0131, 0131, 0101, 0071, 0071,        /* ASL, ASR, AND,  OR, XOR    */
  206.   0111, 0111, 0121, 0121, 0121,    0121,    /*  EQ,  NE,  LT,  LE,  GE,  GT    */
  207.   0061, 0051, 0041, 0041, 0031,        /* ANA, ORO, QUE, COL, CMA    */
  208. /*
  209.  * Unary op's follow
  210.  */
  211.   0160, 0160, 0160, 0160,        /* NEG, PLU, COM, NOT        */
  212.   0170, 0013, 0023,            /* LPA, RPA, END        */
  213. };
  214. /*
  215.  * OP_QUE and OP_RPA have alternate precedences:
  216.  */
  217. #define    OP_RPA_PREC    0013
  218. #define OP_QUE_PREC    0034
  219.  
  220. /*
  221.  * S_ANDOR and S_QUEST signal "short-circuit" boolean evaluation, so that
  222.  *    #if FOO != 0 && 10 / FOO ...
  223.  * doesn't generate an error message.  They are stored in optab.skip.
  224.  */
  225. #define S_ANDOR        2
  226. #define S_QUEST        1
  227.  
  228. typedef struct optab {
  229.     char    op;            /* Operator            */
  230.     char    prec;            /* Its precedence        */
  231.     char    skip;            /* Short-circuit: TRUE to skip    */
  232. } OPTAB;
  233. static int    evalue;            /* Current value from evallex()    */
  234.  
  235. #ifdef    nomacargs
  236. FILE_LOCAL int
  237. isbinary(op)
  238. register int    op;
  239. {
  240.     return (op >= FIRST_BINOP && op <= LAST_BINOP);
  241. }
  242.  
  243. FILE_LOCAL int
  244. isunary(op)
  245. register int    op;
  246. {
  247.     return (op >= FIRST_UNOP && op <= LAST_UNOP);
  248. }
  249. #else
  250. #define    isbinary(op)    (op >= FIRST_BINOP && op <= LAST_BINOP)
  251. #define    isunary(op)    (op >= FIRST_UNOP  && op <= LAST_UNOP)
  252. #endif
  253.  
  254. /*
  255.  * The following definitions are used to specify basic variable sizes.
  256.  */
  257.  
  258. #ifndef    S_CHAR
  259. #define    S_CHAR        (sizeof (char))
  260. #endif
  261. #ifndef    S_SINT
  262. #define    S_SINT        (sizeof (short int))
  263. #endif
  264. #ifndef    S_INT
  265. #define    S_INT        (sizeof (int))
  266. #endif
  267. #ifndef    S_LINT
  268. #define    S_LINT        (sizeof (long int))
  269. #endif
  270. #ifndef    S_FLOAT
  271. #define    S_FLOAT        (sizeof (float))
  272. #endif
  273. #ifndef    S_DOUBLE
  274. #define    S_DOUBLE    (sizeof (double))
  275. #endif
  276. #ifndef    S_PCHAR
  277. #define    S_PCHAR        (sizeof (char *))
  278. #endif
  279. #ifndef    S_PSINT
  280. #define    S_PSINT        (sizeof (short int *))
  281. #endif
  282. #ifndef    S_PINT
  283. #define    S_PINT        (sizeof (int *))
  284. #endif
  285. #ifndef    S_PLINT
  286. #define    S_PLINT        (sizeof (long int *))
  287. #endif
  288. #ifndef    S_PFLOAT
  289. #define    S_PFLOAT    (sizeof (float *))
  290. #endif
  291. #ifndef    S_PDOUBLE
  292. #define    S_PDOUBLE    (sizeof (double *))
  293. #endif
  294. #ifndef    S_PFPTR
  295. #define S_PFPTR        (sizeof (int (*)()))
  296. #endif
  297.  
  298. typedef struct types {
  299.     short    type;            /* This is the bit if        */
  300.     char    *name;            /* this is the token word    */
  301. } TYPES;
  302.  
  303. static TYPES basic_types[] = {
  304.     { T_CHAR,    "char",        },
  305.     { T_INT,    "int",        },
  306.     { T_FLOAT,    "float",    },
  307.     { T_DOUBLE,    "double",    },
  308.     { T_SHORT,    "short",    },
  309.     { T_LONG,    "long",        },
  310.     { T_SIGNED,    "signed",    },
  311.     { T_UNSIGNED,    "unsigned",    },
  312.     { 0,        NULL,        },    /* Signal end        */
  313. };
  314.  
  315. /*
  316.  * Test_table[] is used to test for illegal combinations.
  317.  */
  318. static short test_table[] = {
  319.     T_FLOAT | T_DOUBLE | T_LONG | T_SHORT,
  320.     T_FLOAT | T_DOUBLE | T_CHAR | T_INT,
  321.     T_FLOAT | T_DOUBLE | T_SIGNED | T_UNSIGNED,
  322.     T_LONG  | T_SHORT  | T_CHAR,
  323.     0                        /* end marker    */
  324. };
  325.  
  326. /*
  327.  * The order of this table is important -- it is also referenced by
  328.  * the command line processor to allow run-time overriding of the
  329.  * built-in size values.  The order must not be changed:
  330.  *    char, short, int, long, float, double (func pointer)
  331.  */
  332. SIZES size_table[] = {
  333.     { T_CHAR,    S_CHAR,        S_PCHAR        },    /* char        */
  334.     { T_SHORT,    S_SINT,        S_PSINT        },    /* short int    */
  335.     { T_INT,    S_INT,        S_PINT        },    /* int        */
  336.     { T_LONG,    S_LINT,        S_PLINT        },    /* long        */
  337.     { T_FLOAT,    S_FLOAT,    S_PFLOAT    },    /* float    */
  338.     { T_DOUBLE,    S_DOUBLE,    S_PDOUBLE    },    /* double    */
  339.     { T_FPTR,    0,        S_PFPTR        },    /* int (*())     */
  340.     { 0,    0,        0        },    /* End of table    */
  341. };
  342.  
  343. extern struct symtab deflist[];
  344.  
  345. char* if_expr;
  346. char* if_express;
  347.  
  348. int
  349. eval()
  350. /*
  351.  * Evaluate an expression.  Straight-forward operator precedence.
  352.  * This is called from control() on encountering an #if statement.
  353.  * It calls the following routines:
  354.  * evallex    Lexical analyser -- returns the type and value of
  355.  *        the next input token.
  356.  * evaleval    Evaluate the current operator, given the values on
  357.  *        the value stack.  Returns a pointer to the (new)
  358.  *        value stack.
  359.  * For compatiblity with older cpp's, this return returns 1 (TRUE)
  360.  * if a syntax error is detected.
  361.  */
  362. {
  363.     register int    op;        /* Current operator        */
  364.     register int    *valp;        /* -> value vector        */
  365.     register OPTAB    *opp;        /* Operator stack        */
  366.     int        prec;        /* Op precedence        */
  367.     int        binop;        /* Set if binary op. needed    */
  368.     int        op1;        /* Operand from stack        */
  369.     int        skip;        /* For short-circuit testing    */
  370.     int        value[NEXP];    /* Value stack            */
  371.     OPTAB        opstack[NEXP];    /* Operand stack        */
  372.     extern int    *evaleval();    /* Does actual evaluation    */
  373.  
  374.     valp = value;
  375.     opp = opstack;
  376.     opp->op = OP_END;        /* Mark bottom of stack        */
  377.     opp->prec = opdope[OP_END];    /* And its precedence        */
  378.     opp->skip = 0;            /* Not skipping now        */
  379.     binop = 0;
  380.     if_express = if_expr;
  381. again:    ;
  382. #ifdef    DEBUG_EVAL
  383.     fprintf(stderr,"In #if at again: skip = %d, binop = %d, line is: %s\n",
  384.         opp->skip, binop, if_expr);
  385. #endif
  386.     if ((op = evallex(opp->skip)) == OP_SUB && binop == 0)
  387.         op = OP_NEG;            /* Unary minus        */
  388.     else if (op == OP_ADD && binop == 0)
  389.         op = OP_PLU;            /* Unary plus        */
  390.     else if (op == OP_FAIL)
  391.         return (1);                /* Error in evallex    */
  392. #ifdef    DEBUG_EVAL
  393.     fprintf(stderr,"op = %s, opdope = %03o, binop = %d, skip = %d\n",
  394.         opname[op], opdope[op], binop, opp->skip);
  395. #endif
  396.     if (op == DIG) {            /* Value?        */
  397.         if (binop != 0) {
  398.         cerror("misplaced constant in #if", NULLST);
  399.         return (1);
  400.         }
  401.         else if (valp >= &value[NEXP-1]) {
  402.         cerror("#if value stack overflow", NULLST);
  403.         return (1);
  404.         }
  405.         else {
  406. #ifdef    DEBUG_EVAL
  407.         fprintf(stderr,"pushing %d onto value stack[%d]\n",
  408.             evalue, valp - value);
  409. #endif
  410.         *valp++ = evalue;
  411.         binop = 1;
  412.         }
  413.         goto again;
  414.     }
  415.     else if (op > OP_END) {
  416.         cerror("Illegal #if line", NULLST);
  417.         return (1);
  418.     }
  419.     prec = opdope[op];
  420.     if (binop != (prec & 1)) {
  421.         cerror("Operator %s in incorrect context", opname[op]);
  422.         return (1);
  423.     }
  424.     binop = (prec & 2) >> 1;
  425.     for (;;) {
  426. #ifdef    DEBUG_EVAL
  427.         fprintf(stderr,"op %s, prec %d., stacked op %s, prec %d, skip %d\n",
  428.         opname[op], prec, opname[opp->op], opp->prec, opp->skip);
  429. #endif
  430.         if (prec > opp->prec) {
  431.         if (op == OP_LPA)
  432.             prec = OP_RPA_PREC;
  433.         else if (op == OP_QUE)
  434.             prec = OP_QUE_PREC;
  435.         op1 = opp->skip;        /* Save skip for test    */
  436.         /*
  437.          * Push operator onto op. stack.
  438.          */
  439.         opp++;
  440.         if (opp >= &opstack[NEXP]) {
  441.             cerror("expression stack overflow at op \"%s\"",
  442.             opname[op]);
  443.             return (1);
  444.         }
  445.         opp->op = op;
  446.         opp->prec = prec;
  447.         skip = (valp>value) && (valp[-1] != 0);/*Short-circuit tester*/
  448.         /*
  449.          * Do the short-circuit stuff here.  Short-circuiting
  450.          * stops automagically when operators are evaluated.
  451.          */
  452.         if ((op == OP_ANA && !skip)
  453.          || (op == OP_ORO && skip))
  454.             opp->skip = S_ANDOR;    /* And/or skip starts    */
  455.         else if (op == OP_QUE)        /* Start of ?: operator    */
  456.             opp->skip = (op1 & S_ANDOR) | ((!skip) ? S_QUEST : 0);
  457.         else if (op == OP_COL) {    /* : inverts S_QUEST    */
  458.             opp->skip = (op1 & S_ANDOR)
  459.                   | (((op1 & S_QUEST) != 0) ? 0 : S_QUEST);
  460.         }
  461.         else {                /* Other ops leave    */
  462.             opp->skip = op1;        /*  skipping unchanged.    */
  463.         }
  464. #ifdef    DEBUG_EVAL
  465.         fprintf(stderr,"stacking %s, valp[-1] == %d at %s\n",
  466.             opname[op], valp[-1], if_expr);
  467.         dumpstack(opstack, opp, value, valp);
  468. #endif
  469.         goto again;
  470.         }
  471.         /*
  472.          * Pop operator from op. stack and evaluate it.
  473.          * End of stack and '(' are specials.
  474.          */
  475.         skip = opp->skip;            /* Remember skip value    */
  476.         switch ((op1 = opp->op)) {        /* Look at stacked op    */
  477.         case OP_END:            /* Stack end marker    */
  478.         if (op == OP_EOE)
  479.             return (valp[-1]);        /* Finished ok.        */
  480.         goto again;            /* Read another op.    */
  481.  
  482.         case OP_LPA:            /* ( on stack        */
  483.         if (op != OP_RPA) {        /* Matches ) on input    */
  484.             cerror("unbalanced paren's, op is \"%s\"", opname[op]);
  485.             return (1);
  486.         }
  487.         opp--;                /* Unstack it        */
  488.         /* goto again;            -- Fall through        */
  489.  
  490.         case OP_QUE:
  491.         goto again;            /* Evaluate true expr.    */
  492.  
  493.         case OP_COL:            /* : on stack.        */
  494.         opp--;                /* Unstack :        */
  495.         if (opp->op != OP_QUE) {    /* Matches ? on stack?    */
  496.             cerror("Misplaced '?' or ':', previous operator is %s",
  497.             opname[opp->op]);
  498.             return (1);
  499.         }
  500.         /*
  501.          * Evaluate op1.
  502.          */
  503.         default:                /* Others:        */
  504.         opp--;                /* Unstack the operator    */
  505. #ifdef    DEBUG_EVAL
  506.         fprintf(stderr,"Stack before evaluation of %s\n", opname[op1]);
  507.         dumpstack(opstack, opp, value, valp);
  508. #endif
  509.         valp = evaleval(valp, op1, skip);
  510. #ifdef    DEBUG_EVAL
  511.         fprintf(stderr,"Stack after evaluation\n");
  512.         dumpstack(opstack, opp, value, valp);
  513. #endif
  514.         }                    /* op1 switch end    */
  515.     }                    /* Stack unwind loop    */
  516. }
  517.  
  518. FILE_LOCAL int
  519. evallex(skip)
  520. int        skip;        /* TRUE if short-circuit evaluation    */
  521. /*
  522.  * Return next eval operator or value.  Called from eval().  It
  523.  * calls a special-purpose routines for 'char' strings and
  524.  * numeric values:
  525.  * evalchar    called to evaluate 'x'
  526.  * evalnum    called to evaluate numbers.
  527.  */
  528. {
  529.     register int    c, c1, t;
  530.     extern struct symtab *lookid();
  531.     struct symtab *l;
  532. again:                      /* Collect the token    */
  533.     c = skipws();
  534.     if ((c = macroid(c)) == EOF_CHAR || c == '\n') {
  535.       unget();
  536.       return (OP_EOE);        /* End of expression    */
  537.     }
  538.     t = type[c];
  539.     if (t == INV) {                /* Total nonsense    */
  540.         if (!skip) {
  541.         if (isascii(c) && isprint(c))
  542.             cerror("illegal character '%c' in #if", c);
  543.         else
  544.             cerror("illegal character (%d decimal) in #if", c);
  545.         }
  546.         return (OP_FAIL);
  547.     }
  548.     else if (t == QUO) {            /* ' or "        */
  549.         if (c == '\'') {            /* Character constant    */
  550.         evalue = evalchar(skip);    /* Somewhat messy    */
  551. #ifdef    DEBUG_EVAL
  552.         fprintf(stderr,"evalchar returns %d.\n", evalue);
  553. #endif
  554.         return (DIG);            /* Return a value    */
  555.         }
  556.         cerror("Can't use a string in an #if", NULLST);
  557.         return (OP_FAIL);
  558.     }
  559.     else if (t == LET) {            /* ID must be a macro    */
  560.         if (streq(tokenbuf, "defined")) {    /* Or defined name    */
  561.         c1 = c = skipws();
  562.         if (c == '(')            /* Allow defined(name)    */
  563.             c = skipws();
  564.         if (type[c] == LET) {
  565.             evalue = (lookid(c) != NULL);
  566.             if (c1 != '('        /* Need to balance    */
  567.              || skipws() == ')')    /* Did we balance?    */
  568.             return (DIG);        /* Parsed ok        */
  569.         }
  570.         cerror("Bad #if ... defined() syntax", NULLST);
  571.         return (OP_FAIL);
  572.         }
  573.         else if (streq(tokenbuf, "sizeof"))    /* New sizeof hackery    */
  574.         return (dosizeof());        /* Gets own routine    */
  575.         evalue = 0;
  576.         return (DIG);
  577.     }
  578.     else if (t == DIG) {            /* Numbers are harder    */
  579.         evalue = evalnum(c);
  580. #ifdef    DEBUG_EVAL
  581.         fprintf(stderr,"evalnum returns %d.\n", evalue);
  582. #endif
  583.     }
  584.     else if (t == SPA) {
  585.       goto again;
  586.     }
  587.     else if (strchr("=<>&|\\!", c) != NULL) {
  588.         /*
  589.          * Process a possible multi-byte lexeme.
  590.          */
  591.         c1 = cget();            /* Peek at next char    */
  592.         switch (c) {
  593.         case '!':
  594.         if (c1 == '=')
  595.             return (OP_NE);
  596.         break;
  597.  
  598.         case '=':
  599.         if (c1 != '=') {        /* Can't say a=b in #if    */
  600.             unget();
  601.             cerror("= not allowed in #if", NULLST);
  602.             return (OP_FAIL);
  603.         }
  604.         return (OP_EQ);
  605.  
  606.         case '>':
  607.         case '<':
  608.         if (c1 == c)
  609.             return ((c == '<') ? OP_ASL : OP_ASR);
  610.         else if (c1 == '=')
  611.             return ((c == '<') ? OP_LE  : OP_GE);
  612.         break;
  613.  
  614.         case '|':
  615.         case '&':
  616.         if (c1 == c)
  617.             return ((c == '|') ? OP_ORO : OP_ANA);
  618.         break;
  619.  
  620.         case '\\':
  621.         if (c1 == '\n')            /* Multi-line if    */
  622.             goto again;
  623.         cerror("Unexpected \\ in #if", NULLST);
  624.         return (OP_FAIL);
  625.         }
  626.         unget();
  627.     }
  628.     return (t);
  629. }
  630.  
  631. FILE_LOCAL int
  632. dosizeof()
  633. /*
  634.  * Process the sizeof (basic type) operation in an #if string.
  635.  * Sets evalue to the size and returns
  636.  *    DIG        success
  637.  *    OP_FAIL        bad parse or something.
  638.  */
  639. {
  640.     register int    c;
  641.     register TYPES    *tp;
  642.     register SIZES    *sizp;
  643.     register short    *testp;
  644.     short        typecode;
  645.  
  646.     if ((c = skipws()) != '(')
  647.         goto nogood;
  648.     /*
  649.      * Scan off the tokens.
  650.      */
  651.     typecode = 0;
  652.     while ((c = skipws())) {
  653.         if ((c = macroid(c)) == EOF_CHAR || c == '\n')
  654.         goto nogood;            /* End of line is a bug    */
  655.         else if (c == '(') {        /* thing (*)() func ptr    */
  656.         if (skipws() == '*'
  657.          && skipws() == ')') {        /* We found (*)        */
  658.             if (skipws() != '(')    /* Let () be optional    */
  659.             unget();
  660.             else if (skipws() != ')')
  661.             goto nogood;
  662.             typecode |= T_FPTR;        /* Function pointer    */
  663.         }
  664.         else {                /* Junk is a bug    */
  665.             goto nogood;
  666.         }
  667.         }
  668.         else if (type[c] != LET)        /* Exit if not a type    */
  669.         break;
  670.         else {                    /* Maybe combine tokens    */
  671.         /*
  672.          * Look for this unexpandable token in basic_types.
  673.          * The code accepts "int long" as well as "long int"
  674.          * which is a minor bug as bugs go (and one shared with
  675.          * a lot of C compilers).
  676.          */
  677.         for (tp = basic_types; tp->name != NULLST; tp++) {
  678.             if (streq(tokenbuf, tp->name))
  679.             break;
  680.         }
  681.         if (tp->name == NULLST) {
  682.             cerror("#if sizeof, unknown type \"%s\"", tokenbuf);
  683.             return (OP_FAIL);
  684.         }
  685.         typecode |= tp->type;        /* Or in the type bit    */
  686.         }
  687.     }
  688.     /*
  689.      * We are at the end of the type scan.  Chew off '*' if necessary.
  690.      */
  691.     if (c == '*') {
  692.         typecode |= T_PTR;
  693.         c = skipws();
  694.     }
  695.     if (c == ')') {                /* Last syntax check    */
  696.         for (testp = test_table; *testp != 0; testp++) {
  697.         if (!bittest(typecode & *testp)) {
  698.             cerror("#if ... sizeof: illegal type combination", NULLST);
  699.             return (OP_FAIL);
  700.         }
  701.         }
  702.         /*
  703.          * We assume that all function pointers are the same size:
  704.          *        sizeof (int (*)()) == sizeof (float (*)())
  705.          * We assume that signed and unsigned don't change the size:
  706.          *        sizeof (signed int) == (sizeof unsigned int)
  707.          */
  708.         if ((typecode & T_FPTR) != 0)    /* Function pointer    */
  709.         typecode = T_FPTR | T_PTR;
  710.         else {                /* Var or var * datum    */
  711.         typecode &= ~(T_SIGNED | T_UNSIGNED);
  712.         if ((typecode & (T_SHORT | T_LONG)) != 0)
  713.             typecode &= ~T_INT;
  714.         }
  715.         if ((typecode & ~T_PTR) == 0) {
  716.         cerror("#if sizeof() error, no type specified", NULLST);
  717.         return (OP_FAIL);
  718.         }
  719.         /*
  720.          * Exactly one bit (and possibly T_PTR) may be set.
  721.          */
  722.         for (sizp = size_table; sizp->bits != 0; sizp++) {
  723.         if ((typecode & ~T_PTR) == sizp->bits) {
  724.             evalue = ((typecode & T_PTR) != 0)
  725.             ? sizp->psize : sizp->size;
  726.             return (DIG);
  727.         }
  728.         }                    /* We shouldn't fail    */
  729.         cerror("#if ... sizeof: bug, unknown type code 0x%x", typecode);
  730.         return (OP_FAIL);
  731.     }
  732.  
  733. nogood:    unget();
  734.     cerror("#if ... sizeof() syntax error", NULLST);
  735.     return (OP_FAIL);
  736. }
  737.  
  738. FILE_LOCAL int
  739. bittest(value)
  740. /*
  741.  * TRUE if value is zero or exactly one bit is set in value.
  742.  */
  743. {
  744. #if (4096 & ~(-4096)) == 0
  745.     return ((value & ~(-value)) == 0);
  746. #else
  747.     /*
  748.      * Do it the hard way (for non 2's complement machines)
  749.      */
  750.     return (value == 0 || value ^ (value - 1) == (value * 2 - 1));
  751. #endif
  752. }
  753.  
  754. FILE_LOCAL int
  755. evalnum(c)
  756. register int    c;
  757. /*
  758.  * Expand number for #if lexical analysis.  Note: evalnum recognizes
  759.  * the unsigned suffix, but only returns a signed int value.
  760.  */
  761. {
  762.     register int    value;
  763.     register int    base;
  764.     register int    c1;
  765.  
  766.     if (c != '0')
  767.         base = 10;
  768.     else if ((c = cget()) == 'x' || c == 'X') {
  769.         base = 16;
  770.         c = cget();
  771.     }
  772.     else base = 8;
  773.     value = 0;
  774.     for (;;) {
  775.         c1 = c;
  776.         if (isascii(c) && isupper(c1))
  777.         c1 = tolower(c1);
  778.         if (c1 >= 'a' && c1 <= 'f')
  779.         c1 -= ('a' - 10);
  780.         else c1 -= '0';
  781.         if (c1 < 0 || c1 >= base)
  782.         break;
  783.         value *= base;
  784.         value += c1;
  785.         c = cget();
  786.     }
  787.  
  788.     if (c == 'u' || c == 'U')    /* Unsigned nonsense        */
  789.         c = cget();
  790.     if (c != EOF_CHAR) if_expr--;
  791.     return (value);
  792. }
  793.  
  794. FILE_LOCAL int
  795. evalchar(skip)
  796. int        skip;        /* TRUE if short-circuit evaluation    */
  797. /*
  798.  * Get a character constant
  799.  */
  800. {
  801.     register int    c;
  802.     register int    value;
  803.     register int    count;
  804.  
  805.     instring = TRUE;
  806.     if ((c = cget()) == '\\') {
  807.         switch ((c = cget())) {
  808.         case 'a':                /* New in Standard    */
  809. #if ('a' == '\a' || '\a' == ALERT)
  810.         value = ALERT;            /* Use predefined value    */
  811. #else
  812.         value = '\a';            /* Use compiler's value    */
  813. #endif
  814.         break;
  815.  
  816.         case 'b':
  817.         value = '\b';
  818.         break;
  819.  
  820.         case 'f':
  821.         value = '\f';
  822.         break;
  823.  
  824.         case 'n':
  825.         value = '\n';
  826.         break;
  827.  
  828.         case 'r':
  829.         value = '\r';
  830.         break;
  831.  
  832.         case 't':
  833.         value = '\t';
  834.         break;
  835.  
  836.         case 'v':                /* New in Standard    */
  837. #if ('v' == '\v' || '\v' == VT)
  838.         value = VT;            /* Use predefined value    */
  839. #else
  840.         value = '\v';            /* Use compiler's value    */
  841. #endif
  842.         break;
  843.  
  844.         case 'x':                /* '\xFF'        */
  845.         count = 3;
  846.         value = 0;
  847.         while ((((c = get()) >= '0' && c <= '9')
  848.              || (c >= 'a' && c <= 'f')
  849.              || (c >= 'A' && c <= 'F'))
  850.             && (--count >= 0)) {
  851.             value *= 16;
  852.             value += (c >= '0' && c <= '9') ?
  853.               (c - '0') : ((c & 0xF) + 9);
  854.         }
  855.         unget();
  856.         break;
  857.  
  858.         default:
  859.         if (c >= '0' && c <= '7') {
  860.             count = 3;
  861.             value = 0;
  862.             while (c >= '0' && c <= '7' && --count >= 0) {
  863.             value *= 8;
  864.             value += (c - '0');
  865.             c = get();
  866.             }
  867.             unget();
  868.         }
  869.         else value = c;
  870.         break;
  871.         }
  872.     }
  873.     else if (c == '\'')
  874.         value = 0;
  875.     else value = c;
  876.     /*
  877.      * We warn on multi-byte constants and try to hack
  878.      * (big|little)endian machines.
  879.      */
  880. #if BIG_ENDIAN
  881.     count = 0;
  882. #endif
  883.     while ((c = get()) != '\'' && c != EOF_CHAR && c != '\n') {
  884.         if (!skip)
  885.         cerror("multi-byte constant '%c' isn't portable", c);
  886. #if BIG_ENDIAN
  887.         count += BITS_CHAR;
  888.         value += (c << count);
  889. #else
  890.         value <<= BITS_CHAR;
  891.         value += c;
  892. #endif
  893.     }
  894.     instring = FALSE;
  895.     return (value);
  896. }
  897.  
  898. FILE_LOCAL int *
  899. evaleval(valp, op, skip)
  900. register int    *valp;
  901. int        op;
  902. int        skip;        /* TRUE if short-circuit evaluation    */
  903. /*
  904.  * Apply the argument operator to the data on the value stack.
  905.  * One or two values are popped from the value stack and the result
  906.  * is pushed onto the value stack.
  907.  *
  908.  * OP_COL is a special case.
  909.  *
  910.  * evaleval() returns the new pointer to the top of the value stack.
  911.  */
  912. {
  913.     register int    v1, v2;
  914.  
  915.     if (isbinary(op))
  916.         v2 = *--valp;
  917.     v1 = *--valp;
  918. #ifdef    DEBUG_EVAL
  919.     fprintf(stderr,"%s op %s", (isbinary(op)) ? "binary" : "unary",
  920.         opname[op]);
  921.     if (isbinary(op))
  922.         fprintf(stderr,", v2 = %d.", v2);
  923.     fprintf(stderr,", v1 = %d.\n", v1);
  924. #endif
  925.     switch (op) {
  926.     case OP_EOE:
  927.          break;
  928.  
  929.     case OP_ADD:
  930.         v1 += v2;
  931.         break;
  932.  
  933.     case OP_SUB:
  934.         v1 -= v2;
  935.         break;
  936.  
  937.     case OP_MUL:
  938.         v1 *= v2;
  939.         break;
  940.  
  941.     case OP_DIV:
  942.     case OP_MOD:
  943.         if (v2 == 0) {
  944.         if (!skip) {
  945.             cerror("%s by zero in #if, zero result assumed",
  946.             (op == OP_DIV) ? "divide" : "mod");
  947.         }
  948.         v1 = 0;
  949.         }
  950.         else if (op == OP_DIV)
  951.         v1 /= v2;
  952.         else
  953.         v1 %= v2;
  954.         break;
  955.  
  956.     case OP_ASL:
  957.         v1 <<= v2;
  958.         break;
  959.  
  960.     case OP_ASR:
  961.         v1 >>= v2;
  962.         break;
  963.  
  964.     case OP_AND:
  965.         v1 &= v2;
  966.         break;
  967.  
  968.     case OP_OR:
  969.         v1 |= v2;
  970.         break;
  971.  
  972.     case OP_XOR:
  973.         v1 ^= v2;
  974.         break;
  975.  
  976.     case OP_EQ:
  977.         v1 = (v1 == v2);
  978.         break;
  979.  
  980.     case OP_NE:
  981.         v1 = (v1 != v2);
  982.         break;
  983.  
  984.     case OP_LT:
  985.         v1 = (v1 < v2);
  986.         break;
  987.  
  988.     case OP_LE:
  989.         v1 = (v1 <= v2);
  990.         break;
  991.  
  992.     case OP_GE:
  993.         v1 = (v1 >= v2);
  994.         break;
  995.  
  996.     case OP_GT:
  997.         v1 = (v1 > v2);
  998.         break;
  999.  
  1000.     case OP_ANA:
  1001.         v1 = (v1 && v2);
  1002.         break;
  1003.  
  1004.     case OP_ORO:
  1005.         v1 = (v1 || v2);
  1006.         break;
  1007.  
  1008.     case OP_COL:
  1009.         /*
  1010.          * v1 has the "true" value, v2 the "false" value.
  1011.          * The top of the value stack has the test.
  1012.          */
  1013.         v1 = (*--valp) ? v1 : v2;
  1014.         break;
  1015.  
  1016.     case OP_NEG:
  1017.         v1 = (-v1);
  1018.         break;
  1019.  
  1020.     case OP_PLU:
  1021.         break;
  1022.  
  1023.     case OP_COM:
  1024.         v1 = ~v1;
  1025.         break;
  1026.  
  1027.     case OP_NOT:
  1028.         v1 = !v1;
  1029.         break;
  1030.  
  1031.     default:
  1032.         cerror("#if bug, operand = %d.", op);
  1033.         v1 = 0;
  1034.     }
  1035.     *valp++ = v1;
  1036.     return (valp);
  1037. }
  1038.  
  1039. #ifdef    DEBUG_EVAL
  1040. dumpstack(opstack, opp, value, valp)
  1041. OPTAB        opstack[NEXP];    /* Operand stack        */
  1042. register OPTAB    *opp;        /* Operator stack        */
  1043. int        value[NEXP];    /* Value stack            */
  1044. register int    *valp;        /* -> value vector        */
  1045. {
  1046.     fprintf(stderr,"index op prec skip name -- op stack at %s", if_expr);
  1047.     while (opp > opstack) {
  1048.         fprintf(stderr," [%2d] %2d  %03o    %d %s\n", opp - opstack,
  1049.         opp->op, opp->prec, opp->skip, opname[opp->op]);
  1050.         opp--;
  1051.     }
  1052.     while (--valp >= value) {
  1053.         fprintf(stderr,"value[%d] = %d\n", (valp - value), *valp);
  1054.     }
  1055. }
  1056. #endif
  1057.  
  1058. /* extras cpp-like functions */
  1059.  
  1060. cerror(format, sarg)
  1061. char        *format;
  1062. char        *sarg;        /* Single string argument        */
  1063. /*
  1064.  * Print a normal error message, string argument.
  1065.  */
  1066. {
  1067.     do_log(format, sarg);
  1068.     fprintf(stderr,"\n");
  1069. }
  1070.  
  1071.  
  1072. cfatal(format, sarg)
  1073. char        *format;
  1074. char        *sarg;        /* Single string argument        */
  1075. /*
  1076.  * Print a normal error message, string argument.
  1077.  */
  1078. {
  1079.     do_log(format, sarg);
  1080.     fprintf(stderr,"\n");
  1081.         exit(1);
  1082. }
  1083.  
  1084.  
  1085. int
  1086. get()
  1087. /*
  1088.  * Return the next character from a macro or the current file.
  1089.  * Handle end of file from #include files.
  1090.  */
  1091. {
  1092.   char c;
  1093.   if ((c = *if_expr) == '\0') return EOF_CHAR;
  1094.   if_expr++;
  1095.   return c;
  1096. }
  1097.  
  1098. unget()
  1099. /*
  1100.  * Backup the pointer to reread the last character.
  1101.  */
  1102. {
  1103.   if (*if_expr != '\0') {
  1104.     if (if_expr >= if_express)
  1105.       if_expr--;
  1106.       else
  1107.     cfatal("Too much pushback", NULLST);
  1108.  
  1109.   }  
  1110. }
  1111.  
  1112.  
  1113. int
  1114. cget()
  1115. /*
  1116.  * Get one character, absorb "funny space" after comments or
  1117.  * token concatenation
  1118.  */
  1119. {
  1120.     register int    c;
  1121.  
  1122.     do {
  1123.         c = get();
  1124.     } while (c == TOK_SEP);
  1125.     return (c);
  1126. }
  1127.  
  1128. int
  1129. macroid(c)
  1130. register int        c;
  1131. /*
  1132.  * If c is a letter, scan the id.  if it's #defined, expand it and scan
  1133.  * the next character and try again.
  1134.  *
  1135.  * Else, return the character.  If type[c] is a LET, the token is in tokenbuf.
  1136.  */
  1137. {
  1138.     register int ct;
  1139.         int cc;
  1140.         cc = c;
  1141.     ct = 0;
  1142.  
  1143.        if (type[cc] != LET) return c;
  1144.        while (type[cc] == LET || type[cc] == DIG) {
  1145.      tokenbuf[ct++] = cc;
  1146.       cc = get();
  1147.     } 
  1148.     tokenbuf[ct] = EOS;
  1149.     if (cc != EOF_CHAR) if_expr--;
  1150.     return (c);
  1151. }
  1152.  
  1153.  
  1154. int
  1155. skipws()
  1156. /*
  1157.  * Skip over whitespace
  1158.  */
  1159. {
  1160.     register int        c;
  1161.     do {                /* Skip whitespace    */
  1162.         c = get();
  1163.     } while (type[c] == SPA);
  1164.     return (c);
  1165. }
  1166.  
  1167.  
  1168. struct symtab *lookid(c)
  1169.   int    c;                /* First character of token    */
  1170. {
  1171.   register struct symtab *val;
  1172.   macroid(c);
  1173.   val = slookup(tokenbuf,deflist);
  1174.   return val;
  1175. }
  1176.